package com.xgjweb.ac.sign
{
	import com.xgjweb.ac.utils.Constant;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import mx.controls.Alert;
	import mx.graphics.codec.JPEGEncoder;
	import mx.managers.CursorManager;
	
	/**
	 * 图片的上传及下载
	 */
	public class Connector
	{
		private var file:FileReference;
		private var myId:String;
		public function Connector()
		{}
		
		//上传图片到服务器
		public function savePic(myData:BitmapData, fun:Function):void{
			var url:String=Constant.ROOT_URL+"signAction.do?method=uploadFile&width="+myData.width+"&height="+myData.height;
 			var request:URLRequest = new URLRequest(url); 			
 			request.method=URLRequestMethod.POST;
		 	request.contentType = "application/octet-stream"; 
 			request.data = new JPEGEncoder(80).encode(myData);
            var loader:URLLoader = new URLLoader();     
            CursorManager.setBusyCursor();             
            loader.load(request);
            loader.addEventListener(Event.COMPLETE,fun);
            loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
		}
		
		//保存图片到本地
		public function downloadFile(myData:BitmapData):void{
			file=new FileReference();
			file.addEventListener(Event.COMPLETE,downloadCompleteHandler);
			file.save(new JPEGEncoder(80).encode(myData),"sign.jpg");
		}
		
		//本地下载成功 
		private function downloadCompleteHandler(event:Event):void{
			Alert.show("成功保存图片到本地！","提示");
		}
		
		//上传服务器IO异常
		private function errorHandler(event:IOErrorEvent):void{
			Alert.show("IO异常，操作失败，请联系管理员！","提示");
		}
	}
}